// WordSpellChecker.cpp : implementation file
//

#include "stdafx.h"
#include "isismarc.h"
#include "WordSpellChecker.h"
#include "MSWordDemo.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CWordSpellChecker dialog


CWordSpellChecker::CWordSpellChecker(CWnd* pParent /*=NULL*/)
	: CDialog(CWordSpellChecker::IDD, pParent)
{
	//{{AFX_DATA_INIT(CWordSpellChecker)
	//}}AFX_DATA_INIT
}


void CWordSpellChecker::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CWordSpellChecker)
	DDX_Control(pDX, IDC_BUTTON1, m_BotonReemplazo);
	DDX_Control(pDX, IDC_LEYENDA, m_Leyenda);
	DDX_Control(pDX, IDC_LIST1, m_ListaSugerencias);
	DDX_Control(pDX, IDC_MSFLEXGRID1, m_GrillaPalabras);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CWordSpellChecker, CDialog)
	//{{AFX_MSG_MAP(CWordSpellChecker)
	ON_WM_CANCELMODE()
	ON_WM_CAPTURECHANGED()
	ON_WM_DESTROY()
	ON_WM_CHAR()
	ON_WM_CHARTOITEM()
	ON_BN_CLICKED(IDC_BUTTON1, OnButtonReplace)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CWordSpellChecker message handlers

BOOL CWordSpellChecker::OnInitDialog() 
{
	CDialog::OnInitDialog();

	m_Leyenda.SetWindowText("");

	//AfxMessageBox(this->recordText);

	this->wordpnt = new CEzWordAutomation(FALSE);
	
	if (vistaduenia->reg_borrado || vistaduenia->reg_bloqueado)
	{
		m_BotonReemplazo.EnableWindow(FALSE);
	}

	SetIcon (AfxGetApp()->LoadIcon(IDI_ICONWORD),false);
	SetWindowText(vistaduenia->base_activa+" - Microsoft Word Spell-Check Service");

	//CEzWordAutomation MsWord(FALSE);

	m_GrillaPalabras.SetRows(0);
	m_GrillaPalabras.SetCols(1);
	m_GrillaPalabras.SetColWidth(0,2850);

	this->tokenizeText();

	for (int i=0; i<palabras.GetSize(); i++)
	{
		m_GrillaPalabras.SetRows(i+1);
		m_GrillaPalabras.SetRow(i);
		
		m_GrillaPalabras.SetCol(0);
		m_GrillaPalabras.SetText(palabras.ElementAt(i));
		m_GrillaPalabras.SetCellAlignment(1);
	}
	
	
	// TODO: Add extra initialization here

	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CWordSpellChecker::OnCancelMode() 
{
	CDialog::OnCancelMode();
	
	// TODO: Add your message handler code here
	
}

void CWordSpellChecker::OnCaptureChanged(CWnd *pWnd) 
{
	// TODO: Add your message handler code here
	
	CDialog::OnCaptureChanged(pWnd);
}

void CWordSpellChecker::SetRecordText(CString texto)
{
	this->recordText = texto;
}

void CWordSpellChecker::tokenizeText()
{

	CString palabra;
	//AfxMessageBox(this->recordText);
	while (this->recordText.GetLength()>0)
	{
		int pos = this->recordText.Find(" ",0);
		if (pos>0)
		{
			palabra = this->recordText.Left(this->recordText.Find(" ",0));
			this->recordText=this->recordText.Mid(this->recordText.Find(" ",0)+1);
		}
		else
		{
			palabra = this->recordText;
			this->recordText="";
		}

		

		if (this->wordpnt->countWordSuggestions(palabra)>0 && this->IsNotPresentInTermList(palabra))
		{
			palabras.Add(palabra);			
		}

	}

}

BEGIN_EVENTSINK_MAP(CWordSpellChecker, CDialog)
    //{{AFX_EVENTSINK_MAP(CWordSpellChecker)
	ON_EVENT(CWordSpellChecker, IDC_MSFLEXGRID1, -600 /* Click */, OnClickMsflexgrid1, VTS_NONE)
	//}}AFX_EVENTSINK_MAP
END_EVENTSINK_MAP()

void CWordSpellChecker::OnClickMsflexgrid1() 
{
	// TODO: Add your control notification handler code here

	
	CArray <CString,CString&> sugerencias;
	sugerencias.RemoveAll();
	m_ListaSugerencias.ResetContent();
	m_Leyenda.SetWindowText("");

	
	CString palabra = m_GrillaPalabras.GetTextMatrix(m_GrillaPalabras.GetRowSel(),0);
	this->wordpnt->returnThreeSuggestions(palabra,sugerencias);

	for (int i=0;i<sugerencias.GetSize();i++)
	{
		m_ListaSugerencias.AddString(sugerencias.GetAt(i));
	}

		
	if (m_ListaSugerencias.GetCount()>0)
		m_ListaSugerencias.SetSel(0);

	
		
	
}

void CWordSpellChecker::OnDestroy() 
{

	vistaduenia->returnFromSyntaxCheck();


	if(NULL != wordpnt)
	{
		wordpnt->ReleaseWord();	
		delete wordpnt;
	}

	CDialog::OnDestroy();

	
	// TODO: Add your message handler code here
	
}

void CWordSpellChecker::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	// TODO: Add your message handler code here and/or call default
	
	CDialog::OnChar(nChar, nRepCnt, nFlags);
}

int CWordSpellChecker::OnCharToItem(UINT nChar, CListBox* pListBox, UINT nIndex) 
{
	// TODO: Add your message handler code here and/or call default
	
	return CDialog::OnCharToItem(nChar, pListBox, nIndex);
}

void CWordSpellChecker::AsignaView(CView * vista)
{

	this->vistaduenia=(CIsismarcView*) vista;
	
}

void CWordSpellChecker::OnButtonReplace() 
{
	// TODO: Add your control notification handler code here
	CString palabra = m_GrillaPalabras.GetTextMatrix(m_GrillaPalabras.GetRowSel(),0);	
	CString reemplazo;
	if (m_ListaSugerencias.GetCurSel()>=0)
	{
		m_ListaSugerencias.GetText(m_ListaSugerencias.GetCurSel(),reemplazo);
		vistaduenia->modificado = true;
		vistaduenia->mi.IsisReplaceText(palabra,reemplazo);
		m_Leyenda.SetWindowText("Replacement of word "+palabra+" has been done");
	}
	else
		m_Leyenda.SetWindowText("ERROR! There is no selected option for replace");

}

//DEL void CWordSpellChecker::returnThreeSuggestions(CString, CArray *)
//DEL {
//DEL 
//DEL }

BOOL CWordSpellChecker::IsNotPresentInTermList(CString palabra)
{
	int i=0;
	while (i<this->palabras.GetSize() && this->palabras.GetAt(i) != palabra)
		i++;

	if (i==palabras.GetSize())
		return true;
	else
		return false;	
}
